From bdbba3fc69d1817d5d9dcdd2bf8b153356cbe805 Mon Sep 17 00:00:00 2001 From: Steve Klabnik Date: Tue, 25 Aug 2015 12:53:18 -0400 Subject: [PATCH] Update guide to use crates.io dependencies Fixes #1930 --- src/doc/guide.md | 91 +++++++++++++++++++++++------------------------- 1 file changed, 43 insertions(+), 48 deletions(-) diff --git a/src/doc/guide.md b/src/doc/guide.md index 3e2ff806d..589322185 100644 --- a/src/doc/guide.md +++ b/src/doc/guide.md @@ -101,17 +101,6 @@ Once you're ready for release, you can use `cargo build --release` to compile yo Compiling hello_world v0.1.0 (file:///path/to/project/hello_world) -## Adding a dependency - -It's quite simple to add a dependency. Simply add it to your `Cargo.toml` file: - -```toml -[dependencies] -time = "0.1.12" -``` - -Re-run `cargo build` to download the dependencies and build your source with the new dependencies. - # Working on an existing Cargo project If you download an existing project that uses Cargo, it's really easy @@ -136,68 +125,74 @@ project. To depend on a library, add it to your `Cargo.toml`. +## Adding a dependency + +It's quite simple to add a dependency. Simply add it to your `Cargo.toml` file: + +```toml +[dependencies] +time = "0.1.12" +``` + +Re-run `cargo build` to download the dependencies and build your source with the new dependencies. + + ```toml [package] name = "hello_world" version = "0.1.0" authors = ["Your Name "] -[dependencies.color] -git = "https://github.com/bjz/color-rs.git" +[dependencies] +regex = "0.1.41" ``` -You added the `color` library, which provides simple conversions -between different color types. +You added the `regex` library, which provides support for regular expressions. Now, you can pull in that library using `extern crate` in `main.rs`. ``` -extern crate color; +extern crate regex; -use color::{Rgb, ToHsv}; +use regex::Regex; fn main() { - println!("Converting RGB to HSV!"); - let red = Rgb::new(255u8, 0, 0); - println!("HSV: {:?}", red.to_hsv::()); + let re = Regex::new(r"^\d{4}-\d{2}-\d{2}$").unwrap(); + println!("Did our date match? {}", re.is_match("2014-01-01")); } ``` -Let's tell Cargo to fetch this new dependency and update the `Cargo.lock`: - -
$ cargo update -p color
-    Updating git repository `https://github.com/bjz/color-rs.git`
+The next time we build, Cargo will fetch this new dependency, all of its +dependencies, compile them all, and update the `Cargo.lock`: -Compile it: +
$ cargo build
+    Updating registry `https://github.com/rust-lang/crates.io-index`
+ Downloading memchr v0.1.5
+ Downloading libc v0.1.10
+ Downloading regex-synatx v0.2.1
+ Downloading memchr v0.1.5
+ Downloading aho-corasick v0.3.0
+ Downloading regex v0.1.41
+   Compiling memchr v0.1.5
+   Compiling libc v0.1.10
+   Compiling regex-synatx v0.2.1
+   Compiling memchr v0.1.5
+   Compiling aho-corasick v0.3.0
+   Compiling regex v0.1.41
+   Compiling foo v0.1.0 (file:///path/to/project/hello_world)
+
+Run it:
 
 
$ cargo run
-   Compiling color v0.1.0 (https://github.com/bjz/color-rs.git#bf739419)
-   Compiling hello_world v0.1.0 (file:///path/to/project/hello_world)
      Running `target/hello_world`
-Converting RGB to HSV!
-HSV: HSV { h: 0, s: 1, v: 1 }
+Did our date match? true
-We just specified a `git` repository for our dependency, but our `Cargo.lock` -contains the exact information about which revision we used: - -```toml -[root] -name = "hello_world" -version = "0.1.0" -dependencies = [ - "color 0.1.0 (git+https://github.com/bjz/color-rs.git#bf739419e2d31050615c1ba1a395b474269a4b98)", -] - -[[package]] -name = "color" -version = "0.1.0" -source = "git+https://github.com/bjz/color-rs.git#bf739419e2d31050615c1ba1a395b474269a4b98" - -``` +Our `Cargo.lock` contains the exact information about which revision of all of +these dependencies we used. -Now, if `color-rs` gets updated, we will still build with the same revision, until -we choose to `cargo update` again. +Now, if `regex` gets updated, we will still build with the same revision, until +we choose to `cargo update`. # Project Layout -- 2.30.2